package com.sukaiyi.giteeideaplugin.component;

import com.intellij.openapi.Disposable;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.text.FontSmoothingType;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;

/**
 * @author sukaiyi
 * @date 2020/07/13
 */
public class JFXStaticWebView extends JFXPanel implements Disposable {

    private static WebView webView;

    private static JFXStaticWebView INSTANCE;

    private JFXStaticWebView() {
        Platform.runLater(() -> {
            if (webView == null) {
                Platform.setImplicitExit(false);
                webView = new WebView();
                webView.getEngine().setUserAgent("GiteeIdeaPlugin");
                webView.setFontSmoothingType(FontSmoothingType.GRAY);
                this.setScene(new Scene(webView));
            }
        });
    }

    public WebView getWebView() {
        return webView;
    }


    public WebEngine getWebEngine() {
        return webView.getEngine();
    }

    public static JFXStaticWebView getInstance() {
        if (INSTANCE == null) {
            synchronized (JFXStaticWebView.class) {
                if (INSTANCE == null) {
                    INSTANCE = new JFXStaticWebView();
                }
            }
        }
        return INSTANCE;
    }

    @Override
    public void dispose() {
    }
}
